
struct Sheep {}
struct Cow {}
trait Animal {
    //约束方法名
    fn sound(&self) -> &'static str;
}

impl Animal for Sheep {
    // 实现接口
    fn sound(&self) -> &'static str {
        "baaah"
    }
}

impl Animal for Cow {
    fn sound(&self) -> &'static str {
        "moooo"
}
}

fn random_animal(num: f64) -> Box<dyn Animal> {
    // randomly return a Box containing a Sheep or a Cow
    if num < 0.5 {
        Box::new(Sheep{})
    }else {
        Box::new(Cow{})
    }

}


fn main(){
    let num = 0.6;
    let animal = random_animal(num);
    println!("animal says {}", animal.sound());

}